package org.aeon.colorfulplanebattle.view.panel;

import org.aeon.colorfulplanebattle.view.GameState;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.SETTLEMENT_Font;
import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.SETTLEMENT_TITLE_Font;
import static org.aeon.colorfulplanebattle.constant.GameStateE.FINISH;
import static org.aeon.colorfulplanebattle.constant.GameStateE.NEW;

/**
 * @author: MissingAeon
 * @date: 2024/1/10 18:24
 * @description:
 */
public class SettlementPage extends JPanel {
    private static final ImageIcon victory;
    private static final ImageIcon fail;

    static {
        InputStream resource = WelcomePage.class.getClassLoader().getResourceAsStream("img/qita/xiaolian.png");
        ImageIcon read = null;
        try {
            if (resource != null) {
                read = new ImageIcon(ImageIO.read(resource).getScaledInstance(50, 50, Image.SCALE_SMOOTH));
            }

        } catch (IOException ignored) {
        }
        victory = (read);
        resource = WelcomePage.class.getClassLoader().getResourceAsStream("img/qita/liulei.png");
        read = null;
        try {
            if (resource != null) {
                read = new ImageIcon(ImageIO.read(resource).getScaledInstance(40, 40, Image.SCALE_SMOOTH));
            }
        } catch (IOException ignored) {
        }
        fail = (read);
    }

    public SettlementPage(GameState gameState) {

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        this.setPreferredSize(new Dimension(300, 200));
        // 查看是否有数据
        if (!(gameState.exist("isVictory") || gameState.exist("score"))) {
            throw new IllegalStateException("未胜利或失败！");
        }
        boolean isVictory = gameState.pop("isVictory");

        // 第一层
        JLabel title = new JLabel(isVictory ? "胜利" : "失败");
        title.setFont(SETTLEMENT_TITLE_Font);

        // 第二层
        JLabel icon = new JLabel(isVictory ? victory : fail);
        int score = gameState.get("score");
        int maxScore = gameState.get("maxScore");
        // JLabel
        JLabel msg = new JLabel(isVictory ? score + "分！恭喜您，突破了自己！" : score + "分！未超过" + maxScore + "分，这次失败了！");
        msg.setFont(SETTLEMENT_Font);

        // 第三层
        JButton newGame = new JButton("重新开始");
        JButton finish = new JButton("结束游戏");

        // 绑定事件
        newGame.addActionListener(e -> gameState.setState(NEW));
        finish.addActionListener(e -> gameState.setState(FINISH));

        // 设置水平和垂直组布局
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGap(40).addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(icon)
                                        .addComponent(title)
                                )
                                .addComponent(msg)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(newGame)
                                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(finish)
                                ))
        );
        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addGap(35)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(icon)
                                .addComponent(title)
                        )
                        .addGap(20)
                        .addComponent(msg)
                        .addGap(40)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(newGame)
                                .addComponent(finish)
                        )
        );
    }
}
